<?php
/**
 * Harliane's constants and utility functions
 *
 * @version $Id: Constants.php,v 1.5 2011-10-02 21:38:17 herve Exp $
 * @copyright 2011
 */

class Constants {

	const ADMIN_USER = "admin";
	const ROOT_USER = "admin";

	const ADMIN_GROUP = "admin";
	const ROOT_GROUP = "root";
	const USER_GROUP = "user";

	const TYPE_USER_TYPE = "type";
	const ADMIN_USER_TYPE = "admin";
	const ADVISER_USER_TYPE = "adviser";

	const TRUE = 1;
	const FALSE = 0;
	
	const SQL_DATE_FORMAT = 'Y-m-d';
	const DISPLAY_DATE_FORMAT = 'd/m/Y';
	const YII_SQL_DATE_FORMAT = 'yyyy-MM-dd';
	const YII_DISPLAY_DATE_FORMAT = 'dd/MM/yyyy';
	
	const SQL_DATETIME_FORMAT = 'Y-m-d H:i:s';
	const DISPLAY_DATETIME_FORMAT = 'd/m/Y H:i';
	const YII_SQL_DATETIME_FORMAT = 'yyyy-MM-dd HH:mm:ss';
	const YII_DISPLAY_DATETIME_FORMAT = 'dd/MM/yyyy HH:mm:ss';
	
	const JUI_DATE_FORMAT = 'dd/mm/yy';
	const JUI_TIME_FORMAT = 'hh:mm:ss';

	private static $types = null;

	private static $languages = null;

	private static $genders = null;
	
	private static $ckeditor_tb = null;

	public static function userTypes() {
		if (self::$types == null)
			self::$types = array (
				self::ADMIN_USER_TYPE => Yii::t('constants', 'Administrator'),
				self::ADVISER_USER_TYPE => Yii::t('constants', 'Adviser')
			);
		return self::$types;
	}

	public static function userLanguages() {
		if (self::$languages == null)
			self::$languages = array (
				'en' => Yii::t('languages', 'English'),
				'fr' => Yii::t('languages', 'French')
			);
		return self::$languages;
	}
	
	public static function ckEditorToolbars() {
		if (self::$ckeditor_tb == null) {
			self::$ckeditor_tb = array(
				array( /* 'Source', '-', */ 'Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript','Superscript','-','RemoveFormat' ),
				array('NumberedList','BulletedList','-','Outdent','Indent','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'),
				array('Format','Font','FontSize', '-', 'TextColor','BGColor', '-', 'Image', 'Link', 'Unlink'),
				array('Cut', 'Copy', 'Paste', '-', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'),
			);
		}
		
		return self::$ckeditor_tb;
	}

	public static function genders() {
		if (self::$genders == null)
			self::$genders = array (
				'mr' => Yii::t('constants', 'Mr'),
				'ms' => Yii::t('constants', 'Ms'),
				'mrs' => Yii::t('constants', 'Mrs')
			);
		return self::$genders;
	}
	
	public static function toSqlDate($dateStr) {
		return date(Constants::SQL_DATE_FORMAT, CDateTimeParser::parse($dateStr, self::YII_DISPLAY_DATE_FORMAT));
	}

	public static function fromSqlDate($dateStr) {
		return date(Constants::DISPLAY_DATE_FORMAT, CDateTimeParser::parse($dateStr, self::YII_SQL_DATE_FORMAT));
	}

	public static function toSqlDateTime($dateStr) {
		return date(Constants::SQL_DATETIME_FORMAT, CDateTimeParser::parse($dateStr, self::YII_DISPLAY_DATETIME_FORMAT));
	}

	public static function fromSqlDateTime($dateStr) {
		return date(Constants::DISPLAY_DATETIME_FORMAT, CDateTimeParser::parse($dateStr, self::YII_SQL_DATETIME_FORMAT));
	}
	
	public static function isNullOrEmpty($str)
	{
		if ($str == null)
			return true;
		
		if (is_string($str))
			return trim($str) === "";
		
		if (is_object($str))
			return Constants::isNullOrEmpty($str->__toString());
		
		return false;
	}

	public static function convertDate($date, $source_format, $display_format) {
		if ($date instanceof Date)
			$d = (int)$date->getTime();
		else 
			$d = CDateTimeParser::parse($date, $source_format);
			return Yii::app()->dateFormatter->format($display_format, $d);
	}

	public static function showSqlDate($date) {
		return self::convertDate($date, self::YII_SQL_DATE_FORMAT, self::YII_DISPLAY_DATE_FORMAT);
	}

	public static function showSqlDateTime($date) {
		return self::convertDate($date, self::YII_SQL_DATETIME_FORMAT, self::YII_DISPLAY_DATETIME_FORMAT);
	}

	public static function findUserType($user) {
		if ($user->adviser != null)
			return self::ADVISER_USER_TYPE;
		else
			return self::ADMIN_USER_TYPE;
	}

	public static function print_var($v, $exit = true) {
		
		if ($exit) die(CVarDumper::dumpAsString($v, 3, true));
		else CVarDumper::dump($v, 3, true);
	}
	
	public static $JuiDatePickerOptions = array(
          'showAnim'=>'fold',
          'duration' => 'fast',
          'altFormat' => Constants::JUI_DATE_FORMAT,
          'dateFormat' => Constants::JUI_DATE_FORMAT,
          'firstDay' => 1,
          'gotoCurrent' => 'true'
      );
	
	public static $JuiDateTimePickerOptions = array(
          'showAnim'=>'fold',
          'duration' => 'fast',
          'altFormat' => Constants::JUI_DATE_FORMAT,
          'dateFormat' => Constants::JUI_DATE_FORMAT,
          'timeFormat' => Constants::JUI_TIME_FORMAT,
          'showSecond' => 'true',
          'firstDay' => 1,
          'gotoCurrent' => 'true'
      );
	
	public static $JuiTimePickerOptions = array(
          'showAnim'=>'fold',
          'duration' => 'fast',
          'timeFormat' => Constants::JUI_TIME_FORMAT,
          'showSecond' => 'true',
          'firstDay' => 1,
          'gotoCurrent' => 'true'
      );
	
}

?>
